//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbLop
    {
        public static DataTable SelectByKhoaHoc(string khoahoc)
        {

            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaKH", Convert.ToInt32(khoahoc));
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectByKhoaHoc", pr);
            return all;
        }
//-----------------------------------------------------------//
        /// <summary>
        public static DataTable SelectLopHocByMaGV(string MaGV)
        {

            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaGV", MaGV);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTKB_SelectLopByMaGV", pr);
            return all;
        }
        /// </summary>
        /// 
        public static DataTable SelectLopHocDiemByMaGV(string MaGV)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaGV", MaGV);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectByMaGV", pr);
            return all;
        }
        /// <returns></returns>
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectAll1", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectTop", pr);
            return all;
        }
        //-----------------------------------------------------------//
        public static DataTable SearchTenLop(ETbLop Lop, String NguoiTao)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLop", Lop.TenLop);
            pr[1] = new SqlParameter(@"NguoiTao", NguoiTao);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_Search", pr);
            return all;
        }

        public static DataTable SelectTuKhoaHoc(int maKH)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"maKH", maKH);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectTuKhoaHoc", pr);
            return all;
        }

        public static DataTable SelectLopHocByKhoaHocMaGV(string khoahoc, string MaGV)
        {

            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"id", Convert.ToInt32(khoahoc));
            pr[1] = new SqlParameter(@"id2", MaGV);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectByKhoaHocByMaGV", pr);
            return all;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu_Sua(ETbLop Lop)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLop", Lop.TenLop);
            pr[1] = new SqlParameter(@"MaLop", Lop.MaLop);

            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_KiemTraDuLieu_Sua", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
        //-----------------------------------------------------------//
        public static bool KiemTraDuLieu(ETbLop Lop)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"TenLop", Lop.TenLop);
            
      
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_KiemTraDuLieu", pr);
            if (all.Rows.Count > 0)
            {
                return true;
            }
            return false;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbLop OTbLop)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"TenLop", OTbLop.TenLop);
            pr[1] = new SqlParameter(@"MaKhoaHoc", OTbLop.MaKhoaHoc);
         
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLop_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbLop OTbLop)
        {
            SqlParameter[] pr = new SqlParameter[3];
            pr[0] = new SqlParameter(@"MaLop", OTbLop.MaLop);
            pr[1] = new SqlParameter(@"TenLop", OTbLop.TenLop);
            pr[2] = new SqlParameter(@"MaKhoaHoc", OTbLop.MaKhoaHoc);
          
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLop_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaLop)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLop",MaLop);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbLop_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbLop SelectByID(int MaLop)
        {
            ETbLop OTbLop = new ETbLop();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLop",MaLop);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbLop_SelectByID", pr);
            if (idr.Read())
              OTbLop = GetOneTbLop(idr);
            idr.Close();
            idr.Dispose();
            return OTbLop;
        }
//-----------------


        public static DataTable SelectByID_Malop(int MaLop)
        {
            
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaLop", MaLop);
           
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbLop_SelectByID", pr);
            return all;
        }
        //------------------------------------------//
        private static ETbLop GetOneTbLop(IDataReader idr)
       {
            ETbLop OTbLop = new ETbLop();
                if (idr["MaLop"] != DBNull.Value)
                    OTbLop.MaLop = (int)idr["MaLop"];
                if (idr["TenLop"] != DBNull.Value)
                    OTbLop.TenLop = (string)idr["TenLop"];
                if (idr["MaKhoaHoc"] != DBNull.Value)
                    OTbLop.MaKhoaHoc = (int)idr["MaKhoaHoc"];
                if (idr["Siso"] != DBNull.Value)
                    OTbLop.Siso = (int)idr["Siso"];
            return OTbLop;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaLop)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaLop",MaLop);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbLop_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbLop> ListAll()
        {
            List<ETbLop> list = new List<ETbLop>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbLop_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbLop(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbLop> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbLop> list = new List<ETbLop>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbLop_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbLop(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}